package com.multysite.quercus;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

@SuppressWarnings("serial")
public class MondayTestServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		MondayScriptingContext context = new MondayScriptingContext();
		context.setUnicodeSemantics(true);
		context.setIni("unicode.semantics", "on");
		context.setScriptEncoding("UTF-8");
		context.init();
		context.start();
		MondayScriptingEngine engine = new MondayScriptingEngine(
				new MondayScriptingFactory(), context);

		String sayHelloVN = "<?php ob_start(); echo  'gà lợn '.$abc; return ob_get_contents(); ?>";
		try {

			MondayCompiledScript compiledscript = engine.compile(sayHelloVN);

			MemcacheService syncCache = MemcacheServiceFactory
					.getMemcacheService();
			syncCache.put("test", compiledscript);
			long begin = System.nanoTime();

			Bindings b = compiledscript.getEngine().getBindings(
					ScriptContext.ENGINE_SCOPE);
			b.put("abc", "def");
			Date date = Calendar.getInstance().getTime();
			b.put("abc", date);

			for (int i = 0; i < 1000; i++) {
				String str;
				try {
					str = (String) compiledscript.eval();
					resp.getWriter().write(str);
					// System.out.println(new
					// String(str.getBytes("iso-8859-1"),"utf-8"));

				} catch (ScriptException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			long end = System.nanoTime();

			System.out.println("\n" + (double) (end - begin) / 1000000000);
		} catch (ScriptException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		//

	}

	public static void main(String[] args) throws ScriptException, IOException {

		//
		// Quercus quercus = new Quercus();
		// quercus.setUnicodeSemantics(true);
		// quercus.setIni("unicode.semantics", "on");
		// quercus.setScriptEncoding("UTF-8");
		// quercus.init();
		// quercus.start();
		// MondayScriptingEngine engine = new MondayScriptingEngine(new
		// MondayScriptingFactory(), quercus);
		//
		// String sayHelloVN =
		// "<?php ob_start(); echo  'gà lợn '.$abc; return ob_get_contents(); ?>";
		//
		//
		// MondayCompiledScript compiledscript = engine.compile(sayHelloVN);
		//
		// MemcacheService syncCache =
		// MemcacheServiceFactory.getMemcacheService();
		// syncCache.put("test", compiledscript);
		// long begin =System.currentTimeMillis();
		//
		// Bindings b =
		// compiledscript.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
		// b.put("abc", "def");
		// Date date = Calendar.getInstance().getTime();
		// b.put("abc", date);
		//
		//
		//
		//
		// // compiledscript.eval();
		// for (int i = 0; i < 1; i++) {
		// String str = (String)compiledscript.eval();
		// System.out.println(new String(str.getBytes("iso-8859-1"),"utf-8"));
		// }
		// long end =System.currentTimeMillis();
		//
		// System.out.println("\n"+(end-begin));
		//

		// ScriptEngineManager manager = new ScriptEngineManager();
		// ScriptEngine enginex = manager.getEngineByName("javascript");
		// engine.put("counter", 0);
		// if (engine instanceof Compilable) {
		// Compilable compEngine = (Compilable) engine;
		// try {
		// CompiledScript script = compEngine.compile("print 'asdasdadad';");
		// long begin =System.currentTimeMillis();
		// for (int i = 0; i < 10000; i++) {
		// script.eval();
		// }
		// long end =System.currentTimeMillis();
		//
		// System.out.println("\n"+(end-begin));
		//
		// } catch (ScriptException e) {
		// System.err.println(e);
		// }
		// } else {
		// System.err.println("Engine can't compile code");
		// }

	}
}
